package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.config.CacheService;
import com.heima.behavior.service.ApCollectionService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.app.UpdateArticleMess;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApCollectionServiceImpl implements ApCollectionService {
    @Autowired
    CacheService cacheService;
    @Autowired
    KafkaTemplate kafkaTemplate;
    /**
     * 收藏
     *
     * @param collectionBehaviorDto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto collectionBehaviorDto) {
        //1.参数校验
        if (collectionBehaviorDto == null || collectionBehaviorDto.getEntryId() == null || checkParm(collectionBehaviorDto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.收藏行为
        Long entryId = collectionBehaviorDto.getEntryId();
        Integer userId = user.getId();
        Short operation = collectionBehaviorDto.getOperation();
        Short type = collectionBehaviorDto.getType();
        if (operation==0){
            //是否已收藏
            Double score = cacheService.zScore(UserRelationConstants.BEHAVIOR_COLLECTION + userId, entryId+ ":" +type);
            if (score!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已收藏");
            }
            //保存收藏数据
            cacheService.zAdd(UserRelationConstants.BEHAVIOR_COLLECTION + userId, entryId+ ":" +type,System.currentTimeMillis());
            log.info("保存收藏数据成功");
            //实时计算文章分值
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
            mess.setArticleId(collectionBehaviorDto.getEntryId());
            kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(mess));
            log.info("*****kafkaTemplate send collection");
        }else {
            cacheService.zRemove(UserRelationConstants.BEHAVIOR_COLLECTION + userId, entryId+ ":" +type);
            log.info("删除收藏数据成功");
        }
        //4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private boolean checkParm(CollectionBehaviorDto collectionBehaviorDto) {
        Short operation = collectionBehaviorDto.getOperation();
        Short type = collectionBehaviorDto.getType();
        if (type > 1 || type < 0 || operation > 1 || operation < 0) {
            return true;
        }
        return false;
    }
}
